package com.zte.market.service.impl;

import com.zte.market.mapper.OrderdetailsMapper;
import com.zte.market.pojo.Orderdetails;
import com.zte.market.service.OrderdetailsService;
import com.zte.market.service.otherService.CProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 许月
 * @Date: 2022/10/05/19:45
 */
@Service
public class OrderdetailsServiceImpl implements OrderdetailsService {

    @Autowired
    private OrderdetailsMapper orderdetailsMapper;  //订单详情的 mapper 接口
    @Autowired
    private CProductService cProductService;  //商品对应的 service 对象


    /**
     * 通过订单 id 查询详情列表
     * @param orderId 订单编号（完整的）
     * @return
     */
    @Override
    public List<Orderdetails> queryByOrderId(String orderId) {
        //获取 订单详情列表
        List<Orderdetails> orderdetailsList = orderdetailsMapper.queryByOrderId(orderId);
        for (Orderdetails orderdetails : orderdetailsList) {
            //设置 商品信息
            orderdetails.setCProduct(cProductService.getByProductId(orderdetails.getProductId()));
        }
        return orderdetailsList;
    }

    /**
     * 批量添加
     * @param orderdetailsList
     * @return
     */
    @Override
    public Integer addByOrderdetailsList(List<Orderdetails> orderdetailsList) {
        Integer rows = 0;
        for (Orderdetails orderdetails : orderdetailsList) {
            Integer result = 0;
            if(orderdetails.getDetailsId() !=null && !"".equals(orderdetails.getDetailsId())){
                //有订单详情id（一般用于修改操作中的添加订单，因为在添加订单详情前已经把所有的相关订单详情删除了）
                //这一步相当于修改操作
                result = orderdetailsMapper.addByOrderdetails(orderdetails);
            }else {
                //没有订单详情id，就进入id自动生成的sql进行添加
                result = orderdetailsMapper.addByOrderdetailsAutodetailsId(orderdetails);
            }
            if(result == 1){
                rows++;
            }
        }
        return rows;
    }



    @Override
    public Integer deleteByDetailsIdList(List<Integer> detailsIdList) {
        Integer rows = orderdetailsMapper.deleteByDetailsIdList(detailsIdList);
        return rows;
    }

    /**
     * 根据订单id删除
     * @param orderId
     * @return
     */
    @Override
    public Integer deleteByOrderId(String orderId) {
        Integer delete = orderdetailsMapper.deleteByOrderId(orderId);
        return delete;
    }
}
